<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入标题"
        v-model="queryForm.title"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.isDrill"
        placeholder="跳转状态"
      >
        <el-option
          v-for="(val, key) in Dicts.CLIENT_IS_DRILL"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.drillKill"
        placeholder="跳转方式"
      >
        <el-option
          v-for="(val, key) in Dicts.CLIENT_DRILL_KILL"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="状态"
      >
        <el-option
          v-for="(val, key) in Dicts.WITHDRAW_MODE_STATUS"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'client:add'"
        class="filter-item"
        @click="openAddDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="编号" width="100" align="center">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="250" align="center">
      </el-table-column>
      <el-table-column
        prop="publishTime"
        label="发布时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="isDrill"
        label="是否跳转"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CLIENT_IS_DRILL[scope.row.isDrill] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="drillKill"
        label="跳转方式"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CLIENT_DRILL_KILL[scope.row.drillKill] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="drillType"
        label="跳转业务"
        width="250"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CLIENT_DRILL_TYPE[scope.row.drillType] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="drillValue"
        label="跳转值"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.WITHDRAW_MODE_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column prop="seq" label="排序号" width="150" align="center">
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'client:detail'"
            size="mini"
            @click="details(scope.row)"
            title="详情"
            icon="el-icon-search"
            circle
          ></el-button>
          <el-button
            v-permission="'client:update'"
            size="mini"
            type="primary"
            @click="editClientHeadline(scope.row.rowId)"
            title="修改"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'client:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <!-- 修改新增弹框 -->
    <el-dialog
      :title="`${editForm.rowId ? '编辑' : '新增'}首页广告`"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      width="60%"
    >
      <el-form
        :model="editForm"
        label-width="140px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-row>
          <el-col :span="22">
            <el-form-item label="标题:" prop="title">
              <el-input v-model="editForm.title" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="状态:" prop="status">
              <el-select
                v-model="editForm.status"
                placeholder="状态"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.WITHDRAW_MODE_STATUS"
                  :key="key"
                  :label="val"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="排序号:" prop="seq">
              <el-input
                v-model="editForm.seq"
                auto-complete="off"
                maxlength="4"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="类型:" prop="type">
              <el-select
                v-model="editForm.type"
                placeholder="类型"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CLIENT_HEADLINE_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="是否跳转:" prop="isDrill">
              <template>
                <el-radio-group v-model="editForm.isDrill" style="width: 100%;">
                  <el-radio :label="1" @click.native.prevent="clickRadio(1)"
                    >是</el-radio
                  >
                  <el-radio :label="0" @click.native.prevent="clickRadio(0)"
                    >否</el-radio
                  >
                </el-radio-group>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="跳转业务:" prop="drillType" v-if="isShow">
              <el-select
                v-model="editForm.drillType"
                placeholder="请选择跳转业务"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CLIENT_DRILL_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="跳转方式:" prop="drillKill" v-if="isShow">
              <el-select
                v-model="editForm.drillKill"
                placeholder="请选择跳转方式"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CLIENT_DRILL_KILL"
                  :key="key"
                  :label="val"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="跳转值:" prop="drillValue" v-if="isShow">
              <el-input
                v-model="editForm.drillValue"
                auto-complete="off"
                placeholder="ID或URL"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item label="描述:" prop="description">
              <el-input
                v-model="editForm.description"
                type="textarea"
                :rows="3"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="图片有效期:" prop="dateTime">
              <el-date-picker
                v-model="editForm.dateTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 100%;"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item label="图片:" prop="pic">
              <el-upload
                v-model="editForm.pic"
                class="upload-demo"
                action=""
                :data="{ dir: '/headlines' }"
                :file-list="files"
                :http-request="upload"
                :on-success="uploadBank"
                :on-remove="removeImg"
                :on-exceed="exceed"
                list-type="picture"
                accept="image/*"
                :limit="1"
              >
                <el-button size="small" type="primary">选择图片</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="saveCilentHeadline(editForm.rowId, dateTime)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog
      :title="`首页广告详情`"
      :close-on-click-modal="false"
      :visible.sync="detailsFormVisible"
      width="55%"
    >
      <el-form :model="editForm" label-width="140px" ref="editForm">
        <el-row>
          <el-col :span="22">
            <el-form-item label="标题:">
              {{ editForm.title }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="状态:">
              {{ Dicts.WITHDRAW_MODE_STATUS[editForm.status] }}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="排序号:">
              {{ editForm.seq }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="类型:" prop="title">
              {{ Dicts.CLIENT_HEADLINE_TYPE[editForm.type] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="是否跳转:">
              <template>
                {{ editForm.isDrill === 1 ? '是' : '否' }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="跳转业务:" v-if="isShow">
              {{ Dicts.CLIENT_DRILL_TYPE[editForm.drillType] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="跳转方式:" v-if="isShow">
              {{ Dicts.CLIENT_DRILL_KILL[editForm.drillKill] }}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="跳转值:" v-if="isShow">
              {{ editForm.drillValue }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item label="描述:">
              {{ editForm.description }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="图片有效期:">
              {{ `${editForm.startTime} - ${editForm.endTime}` }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="图片:">
              <template slot-scope="scope">
                <img
                  :src="$oss(editForm.pic)"
                  alt
                  style="width: 225px;height: 150px"
                />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryClientHeadline,
  detailClientHeadline,
  addOrEditClientHeadline,
  deleteClientHeadline
} from '@/api/terminal'

export default {
  name: 'clientHeadline',
  data() {
    var checkStartNum = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入排序号'))
      } else {
        if (!/^\d+$/.test(value)) {
          callback(new Error('请输入整数'))
        }
        callback()
      }
    }
    return {
      files: [],
      dialogFormVisible: false,
      drillTypeVisible: false,
      detailsFormVisible: false,
      loading: false,
      isShow: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        status: undefined,
        title: undefined
      },
      editForm: {
        rowId: 0,
        title: '',
        pic: '',
        description: '',
        isDrill: 0,
        drillKill: '',
        drillType: '',
        drillValue: '',
        status: '',
        seq: '',
        startTime: '',
        endTime: '',
        dateTime: ''
      },
      editFormRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        pic: [{ required: true, message: '选择图片', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        drillKill: [
          { required: true, message: '请选择跳转方式', trigger: 'blur' }
        ],
        drillType: [
          { required: true, message: '请选择跳转业务', trigger: 'blur' }
        ],
        drillValue: [
          { required: true, message: '请输入跳转值', trigger: 'blur' }
        ],
        seq: [{ required: true, validator: checkStartNum, trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
        dateTime: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ]
      },
      dateTime: '',
      records: [],
      dates: []
    }
  },
  created() {
    // 初始化一次oss client,如果初始化过一次返回上一次初始化对象
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    upload(options) {
      this.uploadForElement(options)
      this.editForm.pic = options.data.dir + '/' + options.file.name
    },
    uploadBank(response, file, fileList) {
      this.editForm.pic = response.name
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryClientHeadline(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClientHeadline(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openAddDialog() {
      this.editForm = {}
      this.editForm = {
        isDrill: 0,
        pic: ''
      }
      this.dialogFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    saveCilentHeadline(rowId) {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.dates = this.editForm.dateTime.splice(',')
          this.editForm.startTime = this.dates[0]
          this.editForm.endTime = this.dates[1]
          if (this.editForm.isDrill === 0) {
            this.editForm.drillKill = ''
            this.editForm.drillType = ''
            this.editForm.drillValue = ''
          }
          addOrEditClientHeadline(this.editForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    clickRadio(param) {
      if (Math.floor(param) === 0) {
        this.editForm.isDrill = Math.floor(param)
        this.isShow = false
      } else {
        this.editForm.isDrill = Math.floor(param)
        this.isShow = true
      }
    },
    editClientHeadline(rowId) {
      detailClientHeadline(rowId).then(({ data: { code, data, msg } }) => {
        this.openAddDialog()
        this.editForm = {
          rowId: data.rowId,
          title: data.title,
          pic: data.pic,
          description: data.description,
          isDrill: data.isDrill,
          drillKill: data.drillKill,
          drillType: data.drillType,
          drillValue: data.drillValue,
          status: data.status,
          seq: data.seq,
          type: data.type,
          dateTime: [data.startTime, data.endTime]
        }
        this.files = [
          { name: this.editForm.pic, url: this.$oss(this.editForm.pic) }
        ]
        this.clickRadio(this.editForm.isDrill)
      })
    },
    details(row) {
      this.detailsFormVisible = true
      this.editForm = {
        rowId: row.rowId,
        title: row.title,
        pic: row.pic,
        description: row.description,
        isDrill: row.isDrill,
        drillKill: row.drillKill,
        drillType: row.drillType,
        drillValue: row.drillValue,
        status: row.status,
        seq: row.seq,
        type: row.type,
        startTime: row.startTime,
        endTime: row.endTime
      }
      this.clickRadio(this.editForm.isDrill)
    },
    exceed() {
      this.$confirm('超出限制，请先删除在选择', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
    },
    removeImg() {
      this.editForm.pic = ''
    }
  },
  components: {}
}
</script>

<style scoped></style>
